#include "SpaceShip.h"
#include "Input.h"
#include "Configuration.h"
#include "Types.h"

void SpaceShip::Display()
{	
	if(!IsAlive)
		return;

	/////////////////////
	Point fireDirection;
	fireDirection.x =1;
	fireDirection.y =0;
	fireDirection.z =0;
	if (PlayerInput::P1_RIGHT.IsPressed && Position.x<Config::TopRightCorner.x-Config::StageBoarderRight)
		Position.x += 1 * Config::GameSpeed;
	if (PlayerInput::P1_LEFT.IsPressed && Position.x > -Config::TopRightCorner.x+Config::StageBoarderLeft)
		Position.x -= 1* Config::GameSpeed;
	if (PlayerInput::P1_UP.IsPressed && Position.y<Config::TopRightCorner.y-Config::StageBoarderTop)
		Position.y += 1* Config::GameSpeed;
	if (PlayerInput::P1_DOWN.IsPressed && Position.y> -Config::TopRightCorner.y+Config::StageBoarderBottom)
		Position.y -= 1* Config::GameSpeed;
	if (PlayerInput::P1_FIRE.IsPressed)
		laser.Fire(Position,fireDirection);

	hitBox.moveToPosition(Position);
	////////////////////
	glPushMatrix();	
	glTranslatef(Position.x,Position.y,Position.z);
	
	glColor3f(ObjectColor.r,ObjectColor.g,ObjectColor.b);
	glBegin(GL_TRIANGLES);
	for (int i = 0 ; i < polygon_qty ; i ++)
	{
		glVertex3f(pVertices[pPolygons[i].a].x,pVertices[pPolygons[i].a].y,pVertices[pPolygons[i].a].z);
		glVertex3f(pVertices[pPolygons[i].b].x,pVertices[pPolygons[i].b].y,pVertices[pPolygons[i].b].z);
		glVertex3f(pVertices[pPolygons[i].c].x,pVertices[pPolygons[i].c].y,pVertices[pPolygons[i].c].z);
	}
	glEnd();
	glPopMatrix();
	EngineOne.DisplayExhaustTrail(Position.x -4,Position.y -1.5,Position.z -4.0);
	EngineTwo.DisplayExhaustTrail(Position.x -4,Position.y -1.5,Position.z +4.0);
	glColor3f(ObjectColor.r+0.5,ObjectColor.g+0.5,ObjectColor.b+0.5);
	laser.Display();
	glColor3f(ObjectColor.r,ObjectColor.g,ObjectColor.b);
	Counter ++;
};

void Engine::DisplayExhaustTrail(float x, float y, float z)
{
	Point enginePosition;
	enginePosition.x =  x;
	enginePosition.y =  y;
	enginePosition.z =  z;

	trail.push_back(enginePosition);
	if (trail.size() <3)
		return;

	for (int i = 0 ; i < trail.size()-1 ; i++)
	{
		glLineWidth(5.0);
		glBegin(GL_LINES);		
		glColor3f(0.9f,0.9f,1.0f);
		glVertex3f(trail[i].x,trail[i].y,trail[i].z);
		glVertex3f(trail[i+1].x,trail[i+1].y,trail[i+1].z);
		glEnd();

		glBegin(GL_LINES);
		glColor3f(FumesColor.r,FumesColor.g,FumesColor.b);
		glVertex3f(trail[i].x,trail[i].y+1,trail[i].z);
		glVertex3f(trail[i+1].x,trail[i+1].y+1,trail[i+1].z);
		glEnd();

		glBegin(GL_LINES);
		glColor3f(FumesColor.r,FumesColor.g,FumesColor.b);
		glVertex3f(trail[i].x,trail[i].y-1,trail[i].z);
		glVertex3f(trail[i+1].x,trail[i+1].y-1,trail[i+1].z);
		glEnd();

		glLineWidth(1.0);

		trail[i].x -= 3*Config::GameSpeed;
		trail[i].y += (rand() %15)/15;
		trail[i].y -= (rand() %15)/15;
		trail[i].z += (rand() %15)/13;
		trail[i].z -= (rand() %15)/13;

		if (trail[i].x < -(Config::TopRightCorner.x + Config::StageBoarderLeft*5))
			trail.erase(trail.begin()+i);
	}
};

void SpaceShip::Initialise()
{
	Position.x = -50;
	Position.y = 0;
	Position.z = -150;
	laser.Initialise();
}

void SpaceShip::Initialise(char* initFile)
{
	Position.x = -50;
	Position.y = 0;
	Position.z = -150;
	laser.Initialise();
	p_3dsFileName = initFile;
	LoadObjectMesh();
	hitBox.LoadAndInitialise("models/shipHB.3ds",Position);
	ObjectColor = Color(0.4,0.4,0.9);
	EngineOne.FumesColor = ObjectColor;
	EngineTwo.FumesColor = ObjectColor;
	health = 100;
	Kills = 0;
}

void SpaceShip::Initialise(char* initFile,Color& color,Point& position)
{
	Position.x = position.x;
	Position.y = position.y;
	Position.z = position.z;
	laser.Initialise();
	p_3dsFileName = initFile;
	LoadObjectMesh();
	hitBox.LoadAndInitialise("models/shipHB.3ds",position);
	ObjectColor = color;
	EngineOne.FumesColor = ObjectColor;
	EngineTwo.FumesColor = ObjectColor;
	health = 100;
	Kills = 0;
}